package com.yc.baseProgram;

//定义一个接口
interface Counter {
	int next();
}

//主类LocalInnerClass
public class LocalInnerClass {
	// 定义一个计数标记count;
	private int count = 0;

	// 使用局部内部类
	Counter getCounter(final String name) {
		class LocalCounter implements Counter {
			public LocalCounter() {
				System.out.println("LocalCount()");
			}

			public int next() {
				System.out.print(name + ":");
				return count++;
			}
		}
		// 局部内部类和匿名内部类最大的不同之处：局部内部类可以返回不止一个内部类的对象
		// 与下面的匿名内部类的代码形成鲜明对比
		return new LocalCounter();
	}

	// 使用匿名内部类
	Counter getCounter2(final String name) {
		return new Counter() {
			{
				System.out.println("Counter");
			}

			@Override
			public int next() {
				System.out.print(name + ":");
				return count++;
			}
		};
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LocalInnerClass lic = new LocalInnerClass();
		// 局部内部类
		Counter c1 = lic.getCounter("Local inner");
		// 匿名内部类
		Counter c2 = lic.getCounter2("Anonymous inner");
		for (int i = 0; i < 5; i++) {
			System.out.println(c1.next());
		}
		for (int j = 0; j < 5; j++) {
			System.out.println(c2.next());
		}
	}

}